// 表单验证
(function(){

    // 显示隐藏
    $(".signin").click(function(){
        this.className="signin focus";
        $(".signup")[0].className="signup";
        $(".input_signin")[0].className="input_signin active";
        $(".input_signup")[0].className="input_signup";
    });
    $(".signup").click(function(){
        this.className="signup focus";
        $(".signin")[0].className="signin";
        $(".input_signup")[0].className="input_signup active";
        $(".input_signin")[0].className="input_signin";
    });

    // 前段表单验证
    var hintText={user_name:{hint:"用户名字",right:"正确",wrong:"请输入用户名"},
        name:{hint:"名字",right:"正确",wrong:"请输入姓名"},
        phone:{hint:"手机",right:"正确",wrong:"请填写手机号"},
        password:{hint:"密码",right:"正确",wrong:"请输入密码"},
        repassword:{hint:"确认密码",right:"正确",wrong:"请确认密码"}};
    var regEvent=function(node, event, func){
        if (node.addEventListener)
            node.addEventListener(event, func);
        else if (node.attachEvent)
            node.attachEvent("on" + event, func);
        else
            node["on" + event] = func;
    };
    function regValue(id,i){
        var flag=false,
            input=document.getElementById(id),
            value=input.value;
        switch (id){
            case "user_name":
            case "login_user_name":
            case "info_user_name":
                flag=/^[a-zA-Z0-9_]{1,20}$/.test(value.replace(/[\u0391-\uFFE5]/g,"nn"));
                id="user_name";
                break;
            case "name":
            case "send_to_name":
            case "send_from_name":
                flag=/^[a-zA-Z ]{1,20}$/.test(value.replace(/[\u0391-\uFFE5]/g,"nn"));
                id="name";
                break;
            case "send_from_address":
            case "send_to_address":
                flag=/^\S{6,16}$/.test(value.replace(/[\u0391-\uFFE5]/g,"nn"));
                id="address";
                break;
            case "password":
            case "login_password":
            case "info_password":
                flag=/^\S{6,16}$/.test(value);
                id="password";
                break;
            case "repassword":
                flag=document.getElementById("password").value==value && value !="" && value !=null && (/^\S{6,16}$/.test(value));
                break;
            case "info_repassword":
                flag=document.getElementById("info_password").value==value && value !="" && value !=null && (/^\S{6,16}$/.test(value));
                id="repassword";
                break;
            case "phone":
            case "info_phone":
            case "send_to_phone":
            case "send_from_phone":
                flag=/^1[3456789]\d{9}$/.test(value);
                id="phone";
                break;
            default:
                break;
        }
        if(flag) {
            index=0;
            input.className="right input";
            hint[i].className="hint hint_right";
            hint[i].innerHTML=hintText[id].right;
        }else{
            input.className="wrong input";
            hint[i].className="hint hint_wrong";
            hint[i].innerHTML=hintText[id].wrong;
            index=1;
        }
    };
    var inputs=document.getElementsByClassName("input"),
        id,
        hint=document.getElementsByClassName("hint"),
        index=0;
    for(var j=0;j<inputs.length;j++) {
        (function(i){
            regEvent(inputs[i],"focus",function(){
                hint[i].style.visibility="visible";
                id=inputs[i].id;
            });
            regEvent(inputs[i],"blur",function(){
                regValue(id,i);
            });
        })(j)
    }
})();